﻿using System;
using System.Collections.Generic;

namespace BankSimulation
{
    public class Program
    {
        public class Bank
        {
            private string name;
            private List<Account> accounts;

            public Bank(string name)
            {
                this.name = name;
                accounts = new List<Account>();
            }

            public void AddAccount(Account account)
            {
                accounts.Add(account);
            }

            public void Withdraw(string accountNumber, decimal amount)
            {
                Account account = accounts.Find(acc => acc.AccountNumber == accountNumber);
                if (account != null)
                {
                    account.Withdraw(amount);
                }
            }
        }

        // 账号类
        public class Account
        {
            public string AccountNumber { get; set; }
            public string AccountHolder { get; set; }
            public decimal Balance { get; protected set; }

            public Account(string accountNumber, string accountHolder, decimal balance)
            {
                AccountNumber = accountNumber;
                AccountHolder = accountHolder;
                Balance = balance;
            }

            public virtual void Withdraw(decimal amount)
            {
                if (amount <= Balance)
                {
                    Balance -= amount;
                    Console.WriteLine($"从账号 {AccountNumber} 提取 {amount:C}");
                }
                else
                {
                    Console.WriteLine("余额不足！");
                }
            }
        }

        // 储蓄账号类，继承自账号类
        public class SavingsAccount : Account
        {
            public SavingsAccount(string accountNumber, string accountHolder, decimal balance)
                : base(accountNumber, accountHolder, balance)
            {

            }

            public override void Withdraw(decimal amount)
            {
                if (amount > 10000)
                {
                    OnBigMoneyFetched(amount);
                }

                base.Withdraw(amount);
            }

            protected virtual void OnBigMoneyFetched(decimal amount)
            {
                BigMoneyArgs args = new BigMoneyArgs(AccountNumber, amount);
                BigMoneyFetched?.Invoke(this, args);
            }

            public event EventHandler<BigMoneyArgs> BigMoneyFetched;
        }

        // 坏钞异常类
        public class BadCashException : Exception
        {
            public BadCashException(string message) : base(message)
            {

            }
        }

        // 大金额参数类
        public class BigMoneyArgs : EventArgs
        {
            public string AccountNumber { get; }
            public decimal Amount { get; }

            public BigMoneyArgs(string accountNumber, decimal amount)
            {
                AccountNumber = accountNumber;
                Amount = amount;
            }
        }
        static void Main(string[] args)
        {
            // 创建银行对象
            Bank bank = new Bank("MyBank");

            // 创建账号对象
            SavingsAccount savingsAccount = new SavingsAccount("12345678", "John Doe", 10000);

            // 注册事件处理程序
            savingsAccount.BigMoneyFetched += HandleBigMoneyFetched;

            // 添加账号到银行
            bank.AddAccount(savingsAccount);

            // 模拟ATM机操作
            try
            {
                int amount = 20000;
                bank.Withdraw("12345678", amount);
            }
            catch (BadCashException ex)
            {
                Console.WriteLine($"ATM异常：{ex.Message}");
            }

            Console.ReadLine();
        }

        static void HandleBigMoneyFetched(object sender, BigMoneyArgs e)
        {
            Console.WriteLine($"告警信息：账号 {e.AccountNumber} 在一次操作中取出了 {e.Amount:C} 的大笔金额！");
        }
    }

    // 银行类
    }

